﻿using System;
using System.IO;
using System.Web;
using System.Web.Mvc;

namespace SmallTests.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult UploadFile()
        {
            ViewBag.FileUploadResult = string.Empty;
            return View();
        }
 
        [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase fileUpload)
        {
            try
            {
                if (fileUpload != null && fileUpload.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(fileUpload.FileName);
                    var path = Path.Combine(Server.MapPath("~/uploads"), fileName);
                    fileUpload.SaveAs(path);
                    ViewBag.FileUploadResult = "Success";
                }
                else
                    ViewBag.FileUploadResult = "No file to upload";
            }
            catch (Exception ex)
            {
                ViewBag.FileUploadResult = string.Format("Error: {0}", ex.Message);
            }

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}
